﻿Namespace UI
	Public Class ApplicationSettings

#Region "Initialization"

		''' <summary>
		''' Loads all the settings to the window.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub ApplicationSettings_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
			With My.Settings
				FadeIn.Value = .DefaultFadeIn
				FadeOut.Value = .DefaultFadeOut
				Volume.Value = .DefaultVolume
				Delay.Value = .DefaultDelay
				DefaultAfterAction.SelectedIndex = .DefaultAfterAction
				RemotePort.Text = .PortNumber
				'DefaultTransportAfterAction.SelectedIndex = .DefaultTransportCueAfterAction
				DefaultTransportAction.SelectedIndex = .DefaultTransportCueAction
				'DefaultStackTransportAction.SelectedIndex = .DefaultStackTransportAction
				FontSizes.Value = .DefaultCueFontSize
				VerboseDebugging.IsChecked = .DebugVerbose
				'ShowConsole.IsChecked = .ShowConsole
				EnableRemoteServer.IsChecked = .EnableRemoteServer
				If .PlayerType = Audio.Players.PlayerTypes.Wave Then
					AudioDeviceSelection.SelectedIndex = 0
				ElseIf .PlayerType = Audio.Players.PlayerTypes.Wasapi Then
					AudioDeviceSelection.SelectedIndex = 1
				ElseIf .PlayerType = Audio.Players.PlayerTypes.Asio Then
					AudioDeviceSelection.SelectedIndex = 2
				End If
				Latency.Value = .PlayerLatency
				AudioDeviceSelection_SelectionChanged(Me, Nothing)
				With My.Application.Show.ShowFile
					cd_01.IsChecked = .Channel01Settings.IsDefault
					cd_23.IsChecked = .Channel23Settings.IsDefault
					cd_45.IsChecked = .Channel45Settings.IsDefault
					cd_67.IsChecked = .Channel67Settings.IsDefault
					ce_01.IsChecked = .Channel01Settings.IsEnabled
					ce_23.IsChecked = .Channel23Settings.IsEnabled
					ce_45.IsChecked = .Channel45Settings.IsEnabled
					ce_67.IsChecked = .Channel67Settings.IsEnabled
				End With
				If .Theme = "Light" Then
					ThemeSelector.SelectedIndex = 0
				ElseIf .Theme = "Dark" Then
					ThemeSelector.SelectedIndex = 1
				End If
				AutoPanDuration.Value = .DefaultTransportAutoPanDuration
                AutoFadeDuration.Value = .DefaultTransportAutoFadeDuration

                KeepBackupFile.IsChecked = .KeepBackupPackage
                OnlyAllowPackages.IsChecked = .OnlyAllowPackages

            End With
		End Sub


#End Region


#Region "Help"

		''' <summary>
		''' Opens context sensitive help for the panel the user is on.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub Help_Click(sender As Object, e As RoutedEventArgs) Handles Help.Click
			Dim hn As System.Windows.Forms.HelpNavigator = Forms.HelpNavigator.Topic
            If Tabs.SelectedIndex = 0 Then
                System.Windows.Forms.Help.ShowHelp(Nothing, My.Application.Info.DirectoryPath & "\Q2Cue Help.chm", hn, "3_ProgramSettings.htm")
            ElseIf Tabs.SelectedIndex = 1 Then
                System.Windows.Forms.Help.ShowHelp(Nothing, My.Application.Info.DirectoryPath & "\Q2Cue Help.chm", hn, "4_CueDefaults.htm")
            ElseIf Tabs.SelectedIndex = 2 Then
                System.Windows.Forms.Help.ShowHelp(Nothing, My.Application.Info.DirectoryPath & "\Q2Cue Help.chm", hn, "5_AudioSettings.htm")
            ElseIf Tabs.SelectedIndex = 3 Then
                System.Windows.Forms.Help.ShowHelp(Nothing, My.Application.Info.DirectoryPath & "\Q2Cue Help.chm", hn, "6_Theme.htm")
            ElseIf Tabs.SelectedIndex = 4 Then
                System.Windows.Forms.Help.ShowHelp(Nothing, My.Application.Info.DirectoryPath & "\Q2Cue Help.chm", hn, "7_FileOptions.htm")
            End If
		End Sub
#End Region

#Region "Functionality"

		''' <summary>
		''' Discards all changes to the settings and closes the window.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub Cancel_Click(sender As Object, e As RoutedEventArgs) Handles Cancel.Click
			My.Settings.Reload()
			Me.DialogResult = False
			Me.Close()
		End Sub

		''' <summary>
		''' Saves all changes to the settings and closes the window.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub Save_Click(sender As Object, e As RoutedEventArgs) Handles Save.Click
            With My.Settings
                .DefaultFadeIn = FadeIn.Value
                .DefaultFadeOut = FadeOut.Value
                .DefaultVolume = Volume.Value
                .DefaultDelay = Delay.Value
                .DefaultAfterAction = DefaultAfterAction.SelectedIndex
                '.DefaultStackTransportAction = DefaultStackTransportAction.SelectedIndex
                .PortNumber = RemotePort.Text
                .DefaultTransportCueAction = DefaultTransportAction.SelectedIndex
                '.DefaultTransportCueAfterAction = DefaultTransportAfterAction.SelectedIndex
                .DefaultCueFontSize = FontSizes.Value
                .DebugVerbose = VerboseDebugging.IsChecked
                '.ShowConsole = ShowConsole.IsChecked
                .EnableRemoteServer = EnableRemoteServer.IsChecked
                With CType(AudioDeviceSelection.SelectedItem, ComboBoxItem)
                    If .Content = "Wave Player" Then
                        My.Settings.PlayerType = Audio.Players.PlayerTypes.Wave
                    ElseIf .Content = "WASAPI Player" Then
                        My.Settings.PlayerType = Audio.Players.PlayerTypes.Wasapi
                    ElseIf .Content = "ASIO Player" Then
                        My.Settings.PlayerType = Audio.Players.PlayerTypes.Asio
                        My.Settings.AsioDriverIndex = ASIODriverSelection.SelectedIndex
                    End If
                End With
                .PlayerLatency = Latency.Value
                .Theme = CType(ThemeSelector.SelectedItem, ComboBoxItem).Content
                .DefaultTransportAutoFadeDuration = AutoFadeDuration.Value
                .DefaultTransportAutoPanDuration = AutoPanDuration.Value

                .OnlyAllowPackages = OnlyAllowPackages.IsChecked
                .KeepBackupPackage = KeepBackupFile.IsChecked

            End With


            With My.Application.Show.ShowFile
				Dim d0 As New Audio.Devices.DeviceChannel(0, Audio.Devices.DeviceChannelType.Stereo, cd_01.IsChecked)
				d0.IsEnabled = ce_01.IsChecked
				Dim d1 As New Audio.Devices.DeviceChannel(1, Audio.Devices.DeviceChannelType.Stereo, cd_23.IsChecked)
				d1.IsEnabled = ce_23.IsChecked
				Dim d2 As New Audio.Devices.DeviceChannel(2, Audio.Devices.DeviceChannelType.Stereo, cd_45.IsChecked)
				d2.IsEnabled = ce_45.IsChecked
				Dim d3 As New Audio.Devices.DeviceChannel(3, Audio.Devices.DeviceChannelType.Stereo, cd_67.IsChecked)
				d3.IsEnabled = ce_67.IsChecked

				.Channel01Settings = d0
				.Channel23Settings = d1
				.Channel45Settings = d2
				.Channel67Settings = d3

			End With

			My.Settings.Save()
			Me.DialogResult = True
			My.Application.Show.Console.SendMessage("Application settings saved.")

			Dim rd As New ResourceDictionary()
			If My.Settings.Theme = "Dark" Then
				rd.Source = New Uri("/Styles/Theme.Dark.xaml", UriKind.Relative)
			ElseIf My.Settings.Theme = "Light" Then
				rd.Source = New Uri("/Styles/Theme.Light.xaml", UriKind.Relative)
			End If
			My.Application.Resources.MergedDictionaries.Insert(1, rd)
			My.Application.Resources.MergedDictionaries.RemoveAt(2)

			Q2Q.Controls.CueList.ForceVisualUpdate(CType(My.Application.MainWindow, DesignWindow).Cues.CueListing)

			Me.Close()
		End Sub

			''' <summary>
			''' Checks to make sure the key isn't a non-numerical key.
			''' </summary>
			''' <param name="sender"></param>
			''' <param name="e"></param>
			''' <remarks></remarks>
		Private Sub RemotePort_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles RemotePort.PreviewKeyDown
			Select Case e.Key
				Case 74 To 83
				Case Key.D0 To Key.D9
				Case Key.Back
				Case Else
					e.Handled = True
			End Select
		End Sub

		''' <summary>
		''' Runs when the audio device selection is changed.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub AudioDeviceSelection_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles AudioDeviceSelection.SelectionChanged
			Try
				If CType(AudioDeviceSelection.SelectedItem, ComboBoxItem).Content = "ASIO Player" Then
					ASIODriverSelection.Items.Clear()
					If NAudio.Wave.AsioOut.GetDriverNames.Count > 0 Then
						For Each s As String In NAudio.Wave.AsioOut.GetDriverNames
							ASIODriverSelection.Items.Add(s)
						Next
						ASIODriverSelection.SelectedIndex = My.Settings.AsioDriverIndex
					Else
						AudioDeviceSelection.SelectedItem = e.RemovedItems.Item(0)
						MsgBox("No ASIO driver could be found on this system. Please make sure you have an ASIO driver installed and functional.", MsgBoxStyle.Exclamation)
						Return
					End If
				End If
			Catch
			End Try
		End Sub


#End Region

	End Class
End Namespace